package CrowdCounting.RunnableTask.CrowdCountingTask.CCShowLabelTaskImp;

import CrowdCounting.Model.CrowdCounting.CrowdCountingDisplayFrame;
import CrowdCounting.RunnableTask.CrowdCountingTask.CCShowLabelTask;
import CrowdCounting.RunnableTask.PythonRunTask;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class CCShowTextLabelTask extends CCShowLabelTask {
    PythonRunTask pythonRunTask;
    BufferedReader pythonProcessTextReader;

    public CCShowTextLabelTask(CrowdCountingDisplayFrame crowdCountingDisplayFrame, JLabel textLabel, PythonRunTask pythonRunTask) {
        this.crowdCountingDisplayFrame = crowdCountingDisplayFrame;
        this.label = textLabel;
        this.pythonRunTask = pythonRunTask;
    }

    public void run() {
        try {
            pythonProcessTextReader = new BufferedReader(new InputStreamReader(pythonRunTask.getProcess().getInputStream(), "gbk"));
            showLabelRunAndRurn();
            pythonProcessTextReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showLabel() {
        try {
            String line;
            while ((line = pythonProcessTextReader.readLine()) != null) {
                System.out.println(line);
                crowdCountingDisplayFrame.displayText(label, line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
